//
//  RootNavigationViewController.m
//  ZPFHimalayan
//
//  Created by zhou on 2018/11/23.
//  Copyright © 2018 MissZhou. All rights reserved.
//

#import "RootNavigationViewController.h"
#import "ZPFRootViewController.h"

@interface RootNavigationViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, weak) id popDelegate;
@property (nonatomic, strong) UIPercentDrivenInteractiveTransition *interactivePopTranstion;
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer *popRecognizer;
@property (nonatomic, assign) BOOL isSystemSildBack;//是否开启系统又滑返回

@end

@implementation RootNavigationViewController

+ (void)initialize
{
    //导航栏主题 title文字属性
    UINavigationBar *navBar = [UINavigationBar appearance];
    //导航栏背景图
    //    [navBar setBackgroundImage:[UIImage imageNamed:@"tabBarBj"] forBarMetrics:UIBarMetricsDefault];
    [navBar setBarTintColor:CNavigationBackGroundColor];
    [navBar setTintColor:CNavigationFontColor];
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:CNavigationFontColor, NSFontAttributeName:SYSTEMFONT(18.0f)}];
    [navBar setBackgroundImage:[UIImage imageWithColor:CNavigationBackGroundColor] forBarMetrics:UIBarMetricsDefault];
    //去阴影线
    [navBar setShadowImage:[UIImage new]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
    //默认开启系统右滑返回
    self.interactivePopGestureRecognizer.enabled = YES;
    self.interactivePopGestureRecognizer.delegate = self;
    //只有在使用转场动画是，禁用系统手势，开启自定义右滑手势
    _popRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleNavigationTransition:)];
    _popRecognizer.edges = UIRectEdgeLeft;
    [_popRecognizer setEnabled:YES];
    [self.view addGestureRecognizer:_popRecognizer];
}

//解决手势失效的问题

//根视图禁止右滑返回
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return self.childViewControllers.count == 1 ? NO : YES;
}

//push时隐藏tabbar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count > 0) {
        //此处未设置XYTransitionProtocol协议转场，日后有需求在添加
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    //修改tabbar的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([viewController isKindOfClass:[ZPFRootViewController class]]) {
        ZPFRootViewController *rootVC = [[ZPFRootViewController alloc] init];
        if (rootVC.isHiddenNavBar) {
            rootVC.view.top = 0;
            [rootVC.navigationController setNavigationBarHidden:YES animated:animated];
        }else{
            rootVC.view.top = SafeAreaTopHeight;
            [rootVC.navigationController setNavigationBarHidden:NO animated:animated];
        }
    }
}

/**
 返回到指定的视图控制器

 @param className 类名
 @param animated 动画
 */
- (BOOL)popToAppointViewController:(NSString *)className animated:(BOOL)animated{
    id vc = [self getCurrentViewControllerClass:className];
    if (vc != nil && [vc isKindOfClass:[UIViewController class]]) {
        [self popToViewController:vc animated:YES];
        return YES;
    }
    return NO;
}

/**
 获得当前导航器显示的视图

 @param className 要获取的视图的名称
 @return 成功返回对应的对象，失败返回nil;
 */
- (instancetype)getCurrentViewControllerClass:(NSString *)className{
    Class classObj = NSClassFromString(className);
    NSArray *szArray = self.viewControllers;
    for (id vc in szArray) {
        if ([vc isKindOfClass:classObj]) {
            return vc;
        }
    }
    return nil;
}

- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}

#pragma mark -- NavitionContollerDelegate
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                         interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
    if (!self.interactivePopTranstion) { return nil; }
    return self.interactivePopTranstion;
}

#pragma mark - UIGestureRecognizer handlrs
- (void)handleNavigationTransition:(UIScreenEdgePanGestureRecognizer *)recognizer{
    CGFloat progress = [recognizer translationInView:self.view].x / (self.view.bounds.size.width);
    //    progress = MIN(1.0, MAX(0.0, progress));
    NSLog(@"右划progress %.2f",progress);
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        self.interactivePopTranstion = [[UIPercentDrivenInteractiveTransition alloc] init];
        [self popViewControllerAnimated:YES];
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged) {
        [self.interactivePopTranstion updateInteractiveTransition:progress];
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
        CGPoint velocity = [recognizer velocityInView:recognizer.view];
        
        if (progress > 0.5 || velocity.x >100) {
            [self.interactivePopTranstion finishInteractiveTransition];
        }
        else {
            [self.interactivePopTranstion cancelInteractiveTransition];
        }
        self.interactivePopTranstion = nil;
    }
}
@end
